package com.tianji.learning.listener;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.entity.mq.SignInMessage;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 添加积分监听器
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class SignRecordListener {

    private final IPointsRecordService recordService;

    /**
     * 添加签到积分
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "sign.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN))
    public void listenSignInRecord(SignInMessage message) {
        log.info("接受签到完成，添加积分消息:{}", message);
        if (message.getUserId() == null || message.getPoints() == null) {
            log.debug("添加签到积分消费者接受到的消息为空");
            return;

        }

        // 添加积分消息-根据积分类型添加积分
        recordService.addPointsRecord(message, PointsRecordType.SIGN);
    }

    /**
     * 添加看完视频积分
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "learned.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.LEARN_SECTION))
    public void listenCourseNewRecord(SignInMessage message) {
        log.info("接受收到视频学习完成，添加积分消息:{}", message);
        if (message.getUserId() == null || message.getPoints() == null) {
            log.debug("添加视频学习积分消费者接受到的消息为空");
            return;

        }

        // 添加积分消息-根据积分类型添加积分
        recordService.addPointsRecord(message, PointsRecordType.LEARNING);
    }

    /**
     * 回答问题积分
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "qa.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY))
    public void listenQARecord(SignInMessage message) {
        log.info("接受收到视频学习完成，添加积分消息:{}", message);
        if (message.getUserId() == null || message.getPoints() == null) {
            log.debug("添加视频学习积分消费者接受到的消息为空");
            return;

        }

        // 添加积分消息-根据积分类型添加积分
        recordService.addPointsRecord(message, PointsRecordType.QA);
    }

    /**
     * 写笔记增加积分
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "note.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_NOTE))
    public void listenNoteRecord(SignInMessage message) {
        log.info("接受收到记笔记完成，添加积分消息:{}", message);
        if (message.getUserId() == null || message.getPoints() == null) {
            log.debug("添加记笔记积分消费者接受到的消息为空");
            return;

        }

        // 添加积分消息-根据积分类型添加积分
        recordService.addPointsRecord(message, PointsRecordType.NOTE);
    }

    /**
     * 课程评价增加积分
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "commit.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.NOTE_GATHERED))
    public void listenNoteReplyRecord(SignInMessage message) {
        log.info("接受收到课程评价完成，添加积分消息:{}", message);
        if (message.getUserId() == null || message.getPoints() == null) {
            log.debug("添加课程评价积分消费者接受到的消息为空");
            return;

        }

        // 添加积分消息-根据积分类型添加积分
        recordService.addPointsRecord(message, PointsRecordType.COMMENT);
    }
}
